var app = getApp();

Page({
  data: {
    ticket_id: '',
    reward_min: '',
    description: '',
    contact: '',
    real_name: '',
    items: [{
      id: 'tel',
      value: '',
      icon: 'icon-phone',
      alert: '请输入手机号',
      checked: false
    },
    {
      id: 'wx',
      value: '',
      icon: 'icon-weixin',
      alert: '请输入微信号',
      checked: false
    }
    ],
    lng: '',
    lat: ''
  },
  onLoad: function (options) {
    var that = this;
    that.resetData();
  },
  onShow: function () {
    var that = this;
    that.resetData();
  },
  ticketIdTap: function (e) {
    this.setData({
      ticket_id: e.detail.value
    })
  },
  rewardMinTap: function (e) {
    if (e.detail.value > 5000) {
      e.detail.value = 5000;
    }
    this.setData({
      reward_min: e.detail.value
    })
  },
  descriptionTap: function (e) {
    this.setData({
      description: e.detail.value
    })
  },
  contactSelect: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.items[index].checked = !that.data.items[index].checked;
    that.setData({
      items: that.data.items
    })
  },
  contactWay: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.items[index].value = e.detail.value;
    that.setData({
      items: that.data.items
    })
  },
  resetData: function () {
    var that = this;
    that.setData({
      ticket_id: '',
      reward_min: '',
      description: '',
      contact: '',
      real_name: wx.getStorageSync("realName"),
      items: [{
        id: 'tel',
        value: wx.getStorageSync("mobile"),
        icon: 'icon-phone',
        alert: '请输入手机号',
        checked: false
      },
      {
        id: 'wx',
        value: wx.getStorageSync("wxid"),
        icon: 'icon-weixin',
        alert: '请输入微信号',
        checked: false
      }
      ]
    })
  },
  sendtap: function () {
    var that = this;

    //判断是否实名认证
    if (wx.getStorageSync("certification") == 'false') {
      wx.showModal({
        title: '提示',
        content: '实名认证用户才能发布!',
        showCancel: false,
        success: function (res) {
          wx.navigateTo({
            url: '../my/pages/certification/certification',
          })
        }
      });
      return false;
    }

    //获取经纬度
    if (that.data.lng == '' || that.data.lat == '') {
      wx.getLocation({
        success: function (res) {
          that.setData({
            lng: res.longitude,
            lat: res.latitude
          });
        },
        complete: function () {
          that.sendData();
        }
      });
    }
  },
  sendData: function () {
    var that = this;

    //判断是否输入客票号
    if (that.data.ticket_id == '') {
      wx.showModal({
        title: '提示',
        content: '请输入客票号!',
        showCancel: false,
        success: function (res) { }
      });
      return false;
    }

    //判断是否输入最低赏金
    if (that.data.reward_min == '') {
      wx.showModal({
        title: '提示',
        content: '请输入最低赏金!',
        showCancel: false,
        success: function (res) { }
      });
      return false;
    }

    //判断是否输入行程描述
    if (that.data.description == '') {
      wx.showModal({
        title: '提示',
        content: '请输入行程描述!',
        showCancel: false,
        success: function (res) { }
      });
      return false;
    }

    //获取联系方式
    var items = that.data.items;
    var contact = '';
    for (var i in items) {
      if (items[i].checked == true) {
        if (items[i].value == '') {
          wx.showModal({
            title: '提示',
            content: items[i].alert,
            showCancel: false,
            success: function (res) { }
          });
          return false;
        } else {
          contact += items[i].id + ":" + items[i].value + "|";
        }
      }
    }
    that.setData({
      contact: contact.substring(0, contact.length - 1)
    });

    //判断是否输入联系方式
    if (that.data.contact == '') {
      wx.showModal({
        title: '提示',
        content: '联系方式不能为空!',
        showCancel: false,
        success: function (res) { }
      });
      return false;
    }

    //发布行程
    var jsonData = {
      ticket_id: that.data.ticket_id,
      real_name: that.data.real_name,
      reward_min: that.data.reward_min,
      description: that.data.description,
      contact: that.data.contact,
      lng: that.data.lng,
      lat: that.data.lat,
    };
    app.showLoading("正在提交...请稍后...");
    app.JsonPost("Api/Trip/add", jsonData, function (data) {
      app.closeLoading();
      if (data !== false) {
        wx.showModal({
          title: '提示',
          content: '发布成功，等待审核',
          showCancel: false,
          success: function (res) {
            that.resetData();
          }
        });
      }
    })
  }
})